#!/usr/bin/python
# -*- coding: utf-8 -*-
#-------------------------------------------------
#Create At: 2009-5-31 上午01:34:34 
#Create By: Fla.sam
#
#DESC: proftpd admin
#
#-------------------------------------------------

from core_app.models import ProFtpdUser
from core_app.models import ProFtpdGroup
#from core_app.models import ProFtpdQuotaLimit
#from core_app.models import ProFtpdQuotaTallies
from core_app.models import Department
from django.contrib import admin
import unipath, os.path
from django.conf import settings

def reset_home_dir( o ):
    '''
    #清理用户的主目录,本操作会删除用户的所有数据
    @param o:
    @type o:
    '''
    user_obj = o
    _h = user_obj.homedir
    _homedir = unipath.Path( _h.encode( settings.FILE_SYSTEM_CODE ) )
        
    _public_dir = unipath.Path( _homedir, settings.FTPD_COMMON_DIR[:-1] )
    _techer_dir = unipath.Path( _homedir, settings.FTPD_TECHER_DIR[:-1] )
    _student_dir = unipath.Path( _homedir, settings.FTPD_STUDENT_DIR[:-1] )
    _user_dir = unipath.Path( _homedir, settings.FTPD_USER_DIR[:-1] )     
    _for_student_dir = unipath.Path( _homedir, settings.FTPD_TECHER_DIR_FOR_STUDENT[:-1] )    
                
    #_user_dir.rmtree( parents = False )
    _homedir.mkdir( parents = True )
    _user_dir.rmtree( parents = False )
    
    #取得用户类型,根据用户类型设置用户的目录树
    if user_obj.usertype == 'S':
        _user_dir.mkdir( parents = True )
        _pub = unipath.Path( settings.FTPD_ROOT_PATH, settings.FTPD_COMMON_DIR )
        _public_dir.make_relative_link_to( str( _pub ) )
        _techer = unipath.Path( settings.FTPD_ROOT_PATH, settings.FTPD_TECHER_DIR )
        _techer_dir.make_relative_link_to( str( _techer ) )
    elif user_obj.usertype == 'T':
        _user_dir.mkdir( parents = True )
        _for_student_dir.mkdir( parents = True )
        _pub = unipath.Path( settings.FTPD_ROOT_PATH, settings.FTPD_COMMON_DIR )
        _public_dir.make_relative_link_to( str( _pub ) )            
    elif user_obj.usertype == 'A':
        pass
    else:
        pass

def make_homedir( o ):
    '''
    #建立用户的主目录结构
    @param o:
    @type o:
    '''
    user_obj = o
    _h = user_obj.homedir
    _homedir = unipath.Path( _h.encode( settings.FILE_SYSTEM_CODE ) )
        
    _public_dir = unipath.Path( _homedir, settings.FTPD_COMMON_DIR[:-1] )
    _techer_dir = unipath.Path( _homedir, settings.FTPD_TECHER_DIR[:-1] )
    _student_dir = unipath.Path( _homedir, settings.FTPD_STUDENT_DIR[:-1] )
    _user_dir = unipath.Path( _homedir, settings.FTPD_USER_DIR[:-1] )         
    _for_student_dir = unipath.Path( _homedir, settings.FTPD_TECHER_DIR_FOR_STUDENT[:-1] )
        
    #_homedir.rmtree( parents = False )
    _homedir.mkdir( parents = True )
    
    #取得用户类型,根据用户类型设置用户的目录树
    if user_obj.usertype == 'S':
        _user_dir.mkdir( parents = True )
        _pub = unipath.Path( settings.FTPD_ROOT_PATH, settings.FTPD_COMMON_DIR )
        try:
            _public_dir.make_relative_link_to( str( _pub ) )
        except OSError:
            pass
        _techer = unipath.Path( settings.FTPD_ROOT_PATH, settings.FTPD_TECHER_DIR )
        try:
            _techer_dir.make_relative_link_to( str( _techer ) )
        except OSError:
            pass
    elif user_obj.usertype == 'T':
        try:
            _user_dir.mkdir( parents = True )
            _for_student_dir.mkdir( parents = True )
        except:
            pass
        _pub = unipath.Path( settings.FTPD_ROOT_PATH, settings.FTPD_COMMON_DIR )
        try:
            _public_dir.make_relative_link_to( str( _pub ) )
        except OSError:
            pass            
    elif user_obj.usertype == 'A':
        pass
    else:
        pass

def mark_enabled( modeladmin, request, queryset ):
    queryset.update( useable = True )
    for user_obj in queryset:
        make_homedir( user_obj )
mark_enabled.short_description = u'标记用户可用'

def mark_disable( modeladmin, request, queryset ):
    queryset.update( useable = False )
mark_disable.short_description = u'标记用户禁止'

def clean_user_directory( modeladmin, request, queryset ):
    for user_obj in queryset:
        reset_home_dir( user_obj )
clean_user_directory.short_description = u'清理用户目录下的所有文件,并重建基本目录'

class ProFtpdUserAdmin( admin.ModelAdmin ):
    actions_on_top = True
    actions_on_bottom = True
    list_display = ( 'userid', \
                    'c_name', \
                    'gender', \
                    'useable', \
                    'usertype',
                    'homedir', \
                    'department', \
                    
                    )
    
    fieldsets = ( 
                  ( u'用户状态:', {'fields':( 'useable', 'usertype', 'userid', 'passwd' )} ),
                  ( u'用户信息:', {'fields':( 'c_name', 'department', 'gender' )} )
                   )
    
    list_filter = ( 'useable', 'usertype' )
    search_fields = ['userid', 'c_name']
    actions = [mark_enabled, mark_disable, clean_user_directory ]    
admin.site.register( ProFtpdUser, ProFtpdUserAdmin )

class ProFtpdGroupAdmin( admin.ModelAdmin ):
    actions_on_top = True
    actions_on_bottom = True
admin.site.register( ProFtpdGroup, ProFtpdGroupAdmin )

#class ProFtpdQuotaLimitAdmin( admin.ModelAdmin ):
#    actions_on_top = True
#    actions_on_bottom = True
#admin.site.register( ProFtpdQuotaLimit, ProFtpdQuotaLimitAdmin )
#
#class ProFtpdQuotaTalliesAdmin( admin.ModelAdmin ):
#    actions_on_top = True
#    actions_on_bottom = True
#admin.site.register( ProFtpdQuotaTallies, ProFtpdQuotaTalliesAdmin )    

class DepartmentAdmin( admin.ModelAdmin ):
    actions_on_top = True
    actions_on_bottom = True
admin.site.register( Department, DepartmentAdmin )


